package com.sxz.sbs.module.client;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.client.adapter.EvidenceAdapter;
import com.sxz.sbs.module.client.bean.EvidenceDetailBean;
import com.sxz.sbs.module.client.presenter.EvidenceDetailPresenter;
import com.sxz.sbs.module.client.view.EvidenceDetailView;
import com.sxz.sbs.module.photo.PhotoViewerActivity;
import com.sxz.sbs.module.photo.PhotoViewerBean;
import com.sxz.sbs.widget.CommonTitle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 定金凭证  合同 列表详情
 *
 * @author tian
 */
public class EvidenceDetailActivity extends BaseActivity<EvidenceDetailPresenter> implements EvidenceDetailView {
    @BindView(R.id.tv_evidence_detail_xq)
    TextView tvEvidenceDetailXq;
    @BindView(R.id.tv_evidence_detail_number)
    TextView tvEvidenceDetailNumber;
    @BindView(R.id.tv_evidence_detail_type)
    TextView tvEvidenceDetailType;
    @BindView(R.id.tv_evidence_detail_size)
    TextView tvEvidenceDetailSize;
    @BindView(R.id.tv_evidence_detail_dj_money)
    TextView tvEvidenceDetailDjMoney;
    @BindView(R.id.rl_evidence_dj)
    LinearLayout rlEvidenceDj;
    @BindView(R.id.tv_evidence_detail_money)
    TextView tvEvidenceDetailMoney;
    @BindView(R.id.tv_evidence_detail_hc_number)
    TextView tvEvidenceDetailHcNumber;
    @BindView(R.id.tv_evidence_detail_hc_type)
    TextView tvEvidenceDetailHcType;
    @BindView(R.id.tv_evidence_detail_hc_page)
    TextView tvEvidenceDetailHcPage;
    @BindView(R.id.rl_evidence_ht)
    LinearLayout rlEvidenceHt;
    @BindView(R.id.rv_evidence_detail)
    RecyclerView rvEvidenceDetail;
    private int evidenceId;
    private int tag;
    private EvidenceAdapter adapter;
    private List<String> list = new ArrayList<>();
    @Override
    protected void init() {
        evidenceId = getIntent().getIntExtra("evidence_id", 0);
        tag = getIntent().getIntExtra("evidence_type", 0);
        if (tag == 1) {
            new CommonTitle.Builder(this).setTitle("定金凭证").build().show();
        } else if (tag == 2) {
            new CommonTitle.Builder(this).setTitle("合同文件").build().show();
            rlEvidenceHt.setVisibility(View.VISIBLE);
            rlEvidenceDj.setVisibility(View.GONE);
        }
        rvEvidenceDetail.setLayoutManager(new GridLayoutManager(this,3));
        adapter = new EvidenceAdapter(this,list);
        rvEvidenceDetail.setAdapter(adapter);
        HashMap<String,Object> map = HttpParamMap.getParamMap();
        map.put("achieveId",evidenceId);
        mPresenter.getEvidenceDetail(map);
        rvEvidenceDetail.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                PhotoViewerBean pvb = new PhotoViewerBean(list,position) ;
                startActivity(new Intent(EvidenceDetailActivity.this, PhotoViewerActivity.class)
                        .putExtra(PhotoViewerActivity.PHOTO_VIEWER_TAG, pvb));
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_evidence_detail;
    }

    @Override
    protected EvidenceDetailPresenter createPresenter() {
        return new EvidenceDetailPresenter();
    }

    @Override
    public void onGetEvidenceDetailSuccess(EvidenceDetailBean data) {
        if (mIsLive && data != null) {
            tvEvidenceDetailXq.setText(data.getParkName());
            tvEvidenceDetailNumber.setText(data.getShowInfoNum());
            tvEvidenceDetailType.setText(data.getSpaceTypeName());
            tvEvidenceDetailSize.setText(data.getSize()+"㎡");
            if(tag == 1) {
                tvEvidenceDetailDjMoney.setText(data.getVal_AdvanceMoney()+"元");
                String[] photoUrl = data.getTxt_DepositVoucher().split("\\|");
                for (int i = 0; i < photoUrl.length; i++) {
                    list.add(photoUrl[i]);
                }
                adapter.notifyDataSetChanged();

            } else if (tag == 2) {
                tvEvidenceDetailMoney.setText(data.getVal_FullPayment()+"元");
                tvEvidenceDetailHcNumber.setText(data.getTxt_Number()+"");
                tvEvidenceDetailHcPage.setText(data.getVal_pageCount()+"");
                tvEvidenceDetailHcType.setText(data.getOctName());

                String[] photoUrl = data.getUrl_PhotoUrl().split("\\|");
                for (int i = 0; i < photoUrl.length; i++) {
                    list.add(photoUrl[i]);
                }
                adapter.notifyDataSetChanged();
            }
        }
    }

}
